package org.jeecg.modules.wx.utils;

import cn.hutool.core.map.MapUtil;
import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName HttpUtil
 * @Description 发起调用api
 * @Author
 * @Data
 **/
@Slf4j
public class HttpUtil {
    public static String post(String url, Map<String,Object> headerMap,Map<String,Object> dataMap) throws Exception {
        HttpRequest httpRequest = HttpRequest.post(url)
                .header("Content-Type", "application/json")
                .header("Accept", "*/*");
        if (headerMap!=null){
            for (Map.Entry<String, Object> entry : headerMap.entrySet()) {
                httpRequest.header(entry.getKey(), entry.getValue().toString());
            }
        }
        log.error("\n 发起请求url信息{}",url);
        log.error("\n 请求体信息{}",JSON.toJSONString(dataMap));
              //返回结果
        String result;
        try{
            result= httpRequest.body(JSON.toJSONString(dataMap))
                    .timeout(30000)//超时，毫秒
                    .execute().body();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new Exception("请求失败");
        }

        log.error("\n 请求返回信息{}",result);
        return result;
    }

    public static String put(String url, Map<String,Object> dataMap) throws Exception {
        HttpRequest httpRequest = HttpRequest.put(url)
                .header("Content-Type", "application/json")
                .header("Accept", "*/*");
        log.error("\n 发起请求url信息{}",url);
        log.error("\n 请求体信息{}",JSON.toJSONString(dataMap));
        //返回结果
        String result;
        try{
            result= httpRequest.body(JSON.toJSONString(dataMap))
                    .timeout(30000)//超时，毫秒
                    .execute().body();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new Exception("请求失败");
        }

        log.error("\n 请求返回信息{}",result);
        return result;
    }

    public static String get(String url, HashMap<String,Object> paramMap,HashMap<String,Object> headerMap) throws Exception {
        if (paramMap!=null && !paramMap.isEmpty()){
            StringBuffer sb = new StringBuffer();
            sb.append("?");
            for (Map.Entry<String, Object> entry : paramMap.entrySet()) {
              sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            sb.deleteCharAt(sb.length()-1);
            url = url + sb;
        }
        log.error("\n 发起请求url信息{}",url);
        HttpRequest httpRequest = HttpRequest.get(url);
        if (headerMap!=null){
            for (Map.Entry<String, Object> entry : headerMap.entrySet()) {
                httpRequest.header(entry.getKey(), entry.getValue().toString());
            }
        }
        String result ;
        try{
            result = httpRequest
                    .timeout(30000)//超时，毫秒
                    .execute().body();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            throw new Exception("请求失败");
        }

        log.error("\n 请求返回信息{}",result);
        return result;
    }

    /**
     * 将参数拼接到get请求的URL里
     *
     * @param sourUrl
     * @param params
     * @return
     */
    public static String asGetUrl(String sourUrl, Map<String, Object> params) {
        if (MapUtil.isEmpty(params)) {
            return sourUrl;
        }
        StringBuilder strUrl = new StringBuilder(sourUrl);
        if (!sourUrl.contains("?")) {
            strUrl.append("?");
        }
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            strUrl.append("&" + entry.getKey() + "=" + entry.getValue());
        }
        return strUrl.toString();
    }

}
